package cn.jbolt.ec.order.handler;

import cn.jbolt.common.model.*;
import cn.jbolt.ec.commodity.CommodityOptionService;

import cn.jbolt.ec.markert.MarketRetailerService;
import cn.jbolt.ec.retailer.RetailerService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;

import java.util.Arrays;

/**
 * A型订单处理类 （正常单）
 */
public class TypeA {
    @Inject
    RetailerService retailerService;

    @Inject
    CommodityOptionService commodityOptionService;
    @Inject
    MarketRetailerService marketRetailerService;


    public int handle(int client_id,int retailer_id,String address,
                      String consignee,
                      String mobile,String commoditiess){


        JSONObject jo = new JSONObject();
        jo.put("amount_total",0);
        jo.put("amount_paid",0);
        jo.put("credit",0);
        jo.put("commission",0);

        int[][] commodities = JSON.parseObject(commoditiess,int[][].class);

        /*
        生成新订单
         */
        BecRetailerOrder becRetailerOrder = new BecRetailerOrder();
        becRetailerOrder.setAddress(address)
                .setMobile(mobile)
                .setConsignee(" "+consignee).setStatus(5)
                .setType(1).setClientId(client_id).setRetailerId(retailer_id)
                .setDiscountAmount(0)
        ;

        String orderids = "j-"+System.currentTimeMillis()+'-'+client_id+'-'+retailer_id;
        becRetailerOrder.setOrderId(orderids);
        if(!becRetailerOrder.save()) return -3; // 订单生成失败


        BecRetailer becRetailer = new BecRetailer().dao().findByIdLoadColumns(retailer_id,
                "id,discount,spreader_credit,platform_commission");



        for(int i=0;i<commodities.length;i++){
              int t1 = handle_commodity(retailer_id,commodities[i],becRetailerOrder,becRetailer,jo);
            if(t1<0) return t1;

        }



        becRetailerOrder.setAmountPaid(jo.getLongValue("amount_paid"))
                .setAmountTotal(jo.getLongValue("amount_total"))
                .setCredit(jo.getIntValue("credit"))
                .setCommission(jo.getIntValue("commission"))
                .setOrderTime(System.currentTimeMillis());

        if(!becRetailerOrder.update()) return -3;

        return becRetailerOrder.getId();



    }

    // 处理当个商品的价格等
    private int handle_commodity(int retailer_id,int[] cmdy,BecRetailerOrder becRetailerOrder,
                                 BecRetailer becRetailer,JSONObject jo){
        BecRetailerOrderCommodity becRetailerOrderCommodity=new BecRetailerOrderCommodity();
        BecRetailerCommodity becRetailerCommodity =
                new BecRetailerCommodity().dao().findByIdLoadColumns(cmdy[0],
                        "id,name,price,retailer_id,special_status,special_price,type_id");
        Kv cond = Kv.by("retailer_id",retailer_id).set("commodity_type_id",becRetailerCommodity.getTypeId());
        BecMarketingRetailer becMarketingRetailer = marketRetailerService.findFirst(cond);
        int discount_ratio = 0;
        int spreader_credit_ratio =0;
        int platform_commission_ratio =0;
        if(becMarketingRetailer!=null && becMarketingRetailer.getStatus()==2){
            discount_ratio = becMarketingRetailer.getDiscount()==null?0:becMarketingRetailer.getDiscount();
            spreader_credit_ratio = becMarketingRetailer.getSpreaderCredit();
            platform_commission_ratio = becMarketingRetailer.getPlatformCommission();
        }else{
            discount_ratio = becRetailer.getDiscount()==null?0:becRetailer.getDiscount();
            spreader_credit_ratio = becRetailer.getSpreaderCredit()==null?0:becRetailer.getSpreaderCredit();
            platform_commission_ratio = becRetailer.getPlatformCommission()==null?0:becRetailer.getPlatformCommission();

        }

        becRetailerOrderCommodity.setAmount(cmdy[1])
                .setRetailerId(retailer_id)
                .setCommodityId(cmdy[0])
                .setCommodityName(becRetailerCommodity.getName())
                .setPrice((long)becRetailerCommodity.getPrice());
        long price = 1000000000000L;

        int special_status = becRetailerCommodity.getSpecialStatus();
        price = becRetailerCommodity.getPrice();

        if(special_status==1 && cmdy[3]==1) {
            becRetailerOrderCommodity.setIsSpecial(true);
            Integer t = becRetailerCommodity.getDiscount();
            if(t!=null && t>-1){
                discount_ratio = t;

                spreader_credit_ratio = becRetailerCommodity.getSpreadCredit()==null?0:becRetailerCommodity.getSpreadCredit();
                platform_commission_ratio = becRetailerCommodity.getPlatformCommission()==null?0: becRetailerCommodity.getPlatformCommission();


            }
             price = becRetailerCommodity.getSpecialPrice();
        }


        System.out.println("discount:"+discount_ratio);


        if(cmdy[2]>0){
            BecRetailerCommodityOption becRetailerCommodityOption =
                    new BecRetailerCommodityOption().dao().findByIdLoadColumns(cmdy[2],
                            "id,title,price,special_price,spreader_credit,platform_commission,discount");


            if(becRetailerCommodityOption==null){
                return  -1; // 规格错误

            }
            price = becRetailerCommodityOption.getPrice();
            if(special_status==1 && cmdy[3]==1) {
                Integer  t= becRetailerCommodityOption.getDiscount();
                becRetailerOrderCommodity.setIsSpecial(true);
                if(t!=null && t>-1){
                    discount_ratio = t;
                    spreader_credit_ratio = becRetailerCommodityOption.getSpreaderCredit()==null?0:becRetailerCommodityOption.getSpreaderCredit();
                    platform_commission_ratio = becRetailerCommodityOption.getPlatformCommission()==null?0:becRetailerCommodityOption.getPlatformCommission();


                }
                 price = becRetailerCommodityOption.getSpecialPrice();
            }

            becRetailerOrderCommodity.setOptionId(cmdy[2]);
            becRetailerOrderCommodity.setOptionTitle(becRetailerCommodityOption.getTitle());
            becRetailerOrderCommodity.setOrderId(becRetailerOrder.getId());
            becRetailerOrderCommodity.setPrice((long)becRetailerCommodityOption.getPrice());


            long t2 = cmdy[1]*price;

            jo.put("amount_paid",jo.getLongValue("amount_paid") + t2*(100- discount_ratio)/100);
            jo.put("amount_total",jo.getLongValue("amount_total")+ t2);
            jo.put("commission",jo.getIntValue("commission")+ t2*platform_commission_ratio/100);
            jo.put("credit",jo.getIntValue("credit")+ t2*spreader_credit_ratio/100);



        }else{
            System.out.println("discount:"+discount_ratio);

            long t3 = cmdy[1]*price;

            jo.put("amount_paid",jo.getLongValue("amount_paid")+ t3*(100- discount_ratio)/100);
            jo.put("amount_total",jo.getLongValue("amount_total")+ t3);
            jo.put("commission",jo.getIntValue("commission")+ t3*platform_commission_ratio/100);
            jo.put("credit",jo.getIntValue("credit")+ t3*spreader_credit_ratio/100);


        }
        System.out.println(jo);

        becRetailerOrderCommodity.setOrderId(becRetailerOrder.getId());
        if(!becRetailerOrderCommodity.save()) return -2;
        return 0;

    }

}
